
// <copyright file="WinFormsHelper.cs" company="Subhadra Software Solutions">
//   
// </copyright>
// <summary>
//   Class WinFormsHelper.
// </summary>

namespace SubhadraSolutions.Sharp.Utils.Windows.Forms
{
    using System.Collections.Generic;
    using System.Linq;
    using System.Reflection;
    using System.Text;
    using System.Windows.Forms;

    using SubhadraSolutions.Sharp.Utils.Reflection;
    using SubhadraSolutions.Sharp.Utils.Validation;

    /// <summary>
    ///     Class WinFormsHelper.
    /// </summary>
    public static class WinFormsHelper
    {
        #region Public Methods and Operators

        /// <summary>
        /// Arranges the columns order.
        /// </summary>
        /// <typeparam name="T">
        /// </typeparam>
        /// <param name="gridView">
        /// The grid view.
        /// </param>
        public static void ArrangeColumnsOrder<T>(DataGridView gridView)
        {
            Guard.ArgumentNotNull(gridView, "gridView");
            List<PropertyInfo> properties = typeof(T).GetProperties(BindingFlags.Instance | BindingFlags.Public).ToList();
            properties.Sort(PropertyOrderComparer.Instance);
            int j = 0;

            for (int i = properties.Count - 1; i >= 0; i--)
            {
                if (gridView.Columns.Contains(properties[i].Name))
                {
                    gridView.Columns[properties[i].Name].DisplayIndex = i - j;
                }
                else
                {
                    j++;
                }
            }
        }

        /// <summary>
        /// Gets the checked items.
        /// </summary>
        /// <typeparam name="T">
        /// </typeparam>
        /// <param name="listBox">
        /// The list box.
        /// </param>
        /// <returns>
        /// List{``0}.
        /// </returns>
        public static List<T> GetCheckedItems<T>(CheckedListBox listBox)
        {
            return (from object item in listBox.CheckedItems select (T)item).ToList();
        }

        /// <summary>
        /// The populate and show open file dialog.
        /// </summary>
        /// <param name="defaultFilePath">
        /// The default file path.
        /// </param>
        /// <param name="multiSelect">
        /// The multi select.
        /// </param>
        /// <param name="checkFileExists">
        /// The check file exists.
        /// </param>
        /// <param name="fileExtensions">
        /// The file extensions.
        /// </param>
        /// <returns>
        /// The <see cref="string[]"/>.
        /// </returns>
        public static string[] PopulateAndShowOpenFileDialog(string defaultFilePath, bool multiSelect, bool checkFileExists, params string[] fileExtensions)
        {
            var dialog = new OpenFileDialog();
            if (!string.IsNullOrEmpty(defaultFilePath))
            {
                dialog.FileName = defaultFilePath;
            }

            dialog.Filter = buildFilter(fileExtensions);
            dialog.Multiselect = multiSelect;
            dialog.CheckPathExists = true;
            dialog.CheckFileExists = checkFileExists;
            if (DialogResult.OK == dialog.ShowDialog())
            {
                Application.DoEvents();
                return dialog.FileNames;
            }

            Application.DoEvents();
            return null;
        }

        /// <summary>
        /// The populate and show save file dialog.
        /// </summary>
        /// <param name="fileExtensions">
        /// The file extensions.
        /// </param>
        /// <returns>
        /// The <see cref="string"/>.
        /// </returns>
        public static string PopulateAndShowSaveFileDialog(params string[] fileExtensions)
        {
            var dialog = new SaveFileDialog { Filter = buildFilter(fileExtensions), CheckPathExists = true, AddExtension = true, OverwritePrompt = true };
            if (DialogResult.OK == dialog.ShowDialog())
            {
                Application.DoEvents();
                return dialog.FileName;
            }

            Application.DoEvents();
            return null;
        }

        /// <summary>
        /// The show folder browser dialog.
        /// </summary>
        /// <param name="showNewFolderButton">
        /// The show new folder button.
        /// </param>
        /// <returns>
        /// The <see cref="string"/>.
        /// </returns>
        public static string ShowFolderBrowserDialog(bool showNewFolderButton)
        {
            var dialog = new FolderBrowserDialog { ShowNewFolderButton = showNewFolderButton };
            if (DialogResult.OK == dialog.ShowDialog())
            {
                Application.DoEvents();
                return dialog.SelectedPath;
            }

            Application.DoEvents();
            return null;
        }

        #endregion Public Methods and Operators

        #region Methods

        /// <summary>
        /// Builds the filter.
        /// </summary>
        /// <param name="fileExtensions">
        /// The file extensions.
        /// </param>
        /// <returns>
        /// System.String.
        /// </returns>
        private static string buildFilter(params string[] fileExtensions)
        {
            var builder = new StringBuilder();
            foreach (string extension in fileExtensions)
            {
                builder.Append("(*.");
                builder.Append(extension);
                builder.Append(")|*.");
                builder.Append(extension);
                builder.Append("|");
            }

            if (fileExtensions.Length > 0)
            {
                builder.Length--;
            }

            return builder.ToString();
        }

        #endregion Methods
    }
}
